package com.github.liuche51.easyTaskL.config;

import com.github.liuche51.easyTask.core.AnnularQueue;
import com.github.liuche51.easyTask.core.EasyTaskConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class EasyTaskLConf {
    private static Logger log = LoggerFactory.getLogger(EasyTaskLConf.class);
    @Value("${easyTaskL.zkAddress}")
    private String zkAddress;
    @Value("${easyTaskL.taskStorePath}")
    private String taskStorePath;
    @Value("${easyTaskL.serverPort}")
    private int serverPort;
    @Value("${easyTaskL.sQLlitePoolSize}")
    private int sQLlitePoolSize;
    @Value("${easyTaskL.backupCount}")
    private int backupCount;
    @Value("${easyTaskL.dispatchPool.corePoolSize}")
    private int dispatchCorePoolSize;
    @Value("${easyTaskL.dispatchPool.maximumPoolSize}")
    private int dispatchMaximumPoolSize;
    @Value("${easyTaskL.workPool.corePoolSize}")
    private int workPoolCorePoolSize;
    @Value("${easyTaskL.workPool.maximumPoolSize}")
    private int workPoolMaximumPoolSize;
    @Bean
    public AnnularQueue initAnnularQueue(){
        try {
            EasyTaskConfig config =new EasyTaskConfig();
            config.setTaskStorePath(taskStorePath);
            config.setServerPort(serverPort);
            config.setSQLlitePoolSize(sQLlitePoolSize);
            //config.setBackupCount(backupCount);
            config.setZkAddress(zkAddress);
            AnnularQueue annularQueue = AnnularQueue.getInstance();
            config.setDispatchs(new ThreadPoolExecutor(dispatchCorePoolSize, dispatchMaximumPoolSize, 1000, java.util.concurrent.TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>()));
            config.setWorkers(new ThreadPoolExecutor(workPoolCorePoolSize, workPoolMaximumPoolSize, 1000, java.util.concurrent.TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>()));
            annularQueue.start(config);
            return annularQueue;
        }catch (Exception e){
            log.error("",e);
             return null;
        }
    }

}
